const {getAllOrders,delOrdersById,getOneOrderById,getSurrenderOrders,
    handelSurrenderOrders,refuseSurrenderOrders,getSaleManOrder,updateOrderBySaleMan,addOrder} =require ('../dao/ordersDao')

module.exports.getAllOrders=async params=>{
   const data = await getAllOrders(params)
   try {
       return{
           message:'获取订单数据成功',
           code:1,
           data
       }
   } catch (error) {
    return{
        message:'获取订单数据失败',
        code:0,
    }
   }
}

module.exports.getSurrenderOrders=async ()=>{
    const data = await getSurrenderOrders()
    // console.log('退保订单',data);
    try {
        return{
            message:'获取退保订单数据成功',
            code:1,
            data
        }
    } catch (error) {
     return{
         message:'获取退保订单数据失败',
         code:0,
     }
    }
 }

 module.exports.handelSurrenderOrders =async params=>{
     const data =await handelSurrenderOrders(params)
    //  console.log(data,'同意退保');
     try {
        return{
            message:'同意退保成功',
            code:1,
            data
        }
    } catch (error) {
     return{
         message:'同意退保失败',
         code:0,
     }
    }
 }
 module.exports.refuseSurrenderOrders =async params=>{
    const data =await refuseSurrenderOrders(params)
    // console.log(data,'拒绝退保');
    try {
        return{
            message:'拒绝退保成功',
            code:1,
            data
        }
    } catch (error) {
     return{
         message:'拒绝退保失败',
         code:0,
     }
    }
}

module.exports.delOrdersById=async params=>{
    const data = await delOrdersById(params)
    if(data.nModified){
        return{
            message:'删除学生成功',
            code:1
        }
    }
    return{
        message:'删除学生失败',
        code:0
    }
 }
//根据订单id获取
 module.exports.getOneOrderById=async params=>{
    const data = await getOneOrderById(params)
    // console.log('getOneOrderById',data);
    if (data.length == 1) {
        return {
            message: '获取成功',
            code: 1,
            data
        }
    } else {
        return {
            message: '获取失败',
            code: 0
        }
    }
 }

 //根据销售员id获取订单
 module.exports.getSaleManOrder=async params=>{
    const data = await getSaleManOrder(params)
    // console.log('getSaleManOrder',data);
    if (data.length >0 ) {
        return {
            message: '获取成功',
            code: 1,
            data
        }
    } else {
        return {
            message: '获取失败',
            code: 0
        }
    }
 }

 module.exports.updateOrderBySaleMan=async params=>{
     const data =await updateOrderBySaleMan(params)
    //  console.log(data,'修改');
     if (data.nModified > 0) {
        return {
            message: '修改成功',
            status: 1,
        }
    } else {
        return {
            message: '修改失败',
            status: 0
        }
    }
 }

 //客户下单
 module.exports.addOrder=async params=>{
     const data =await addOrder(params)
     if(data._id){
        return {data,message:'下单成功',status:1}
    }else{
        return {data,message:'下单失败',status:0}
    }
 }